<?php
 
require_once(dirname(__FILE__) . '/AmazonSigned.php');
 
class AmazonProductAPI
{
 
    private $public_key;
    private $private_key;
 
    const MUSIC = "Music";
    const DVD   = "DVD";
    const GAMES = "VideoGames";
	
	public function __construct($public_key, $private_key)
	{
		$this->public_key = $public_key;
		$this->private_key = $private_key;
	}
 
    private function verifyXmlResponse($response)
    {
        if ($response === False)
        {
            throw new Exception("Could not connect to Amazon");
        }
        else
        {
            if (isset($response->Items->Item->ItemAttributes->Title))
            {
                return ($response);
            }
            else
            {
                throw new Exception("Invalid xml response.");
            }
        }
    }
 
    private function queryAmazon($parameters)
    {
        return aws_signed_request("jp",
                                  $parameters,
                                  $this->public_key,
                                  $this->private_key);
    }
 
    public function searchAuthor($author)
    {
		$parameters = array("Operation"     => "ItemSearch",
							"Author"        => $author,
							"SearchIndex"   => "Books");
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
    }
	
    public function getSimiliar($asin)
    {
		$parameters = array("Operation"     => "SimilarityLookup",
							"ItemId"        => $asin);
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
    }
}
 
?>